package com.hdy.orderforthree.widget;

import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hdy.net.order.NetCallback;
import com.hdy.orderforthree.mvp.TitleBarPresenter;

import butterknife.ButterKnife;


public class BasePop {

    protected Context context;
    protected ViewGroup mContainer;
    protected View mLayout;
    protected NetCallback netCallback;

    protected TitleBarPresenter.UpdateViewCallback mUpdateViewCallback;

    public BasePop(int containerResId, int addLayoutId, Context context) {
        this.context = context;
        mContainer = ((Activity) context).findViewById(containerResId);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mLayout = inflater.inflate(addLayoutId, mContainer, false);

        ButterKnife.bind(this, mLayout);
        initAnim();
    }

    protected void initAnim() {
        LayoutTransition anim = new LayoutTransition();
        PropertyValuesHolder scaleInX = PropertyValuesHolder.ofFloat("scaleX", 0.8f, 1f);
        PropertyValuesHolder scaleInY = PropertyValuesHolder.ofFloat("scaleY", 0.8f, 1f);
        PropertyValuesHolder alphaIn = PropertyValuesHolder.ofFloat("alpha", 0.5f, 1f);
        PropertyValuesHolder scaleOutX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.8f);
        PropertyValuesHolder scaleOutY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.8f);
        PropertyValuesHolder alphaOut = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);

        ObjectAnimator in = ObjectAnimator.ofPropertyValuesHolder(mLayout, scaleInX, scaleInY, alphaIn);
        ObjectAnimator out = ObjectAnimator.ofPropertyValuesHolder(mLayout, scaleOutX, scaleOutY, alphaOut);
        anim.setAnimator(LayoutTransition.APPEARING, in);
        anim.setAnimator(LayoutTransition.DISAPPEARING, out);
        anim.setDuration(500);
        anim.addTransitionListener(new LayoutTransition.TransitionListener() {
            @Override
            public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {

            }

            @Override
            public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
            }
        });
        mContainer.setLayoutTransition(anim);
    }

    public synchronized void addView() {
        if (isViewCreated()) {
            initAnim();
            mContainer.addView(mLayout);
        }
    }

    public synchronized void removeView() {
        if (isViewCreated()) {
            mContainer.removeView(mLayout);
        }
    }

    protected boolean isViewCreated() {
        return mContainer != null
                && mLayout != null;
    }

    public boolean isShowing() {
        if (mContainer != null && mLayout != null) {
            for (int i=0; i < mContainer.getChildCount(); i++) {
                if (mContainer.getChildAt(i) == mLayout) {
                    return true;
                }
            }
        }
        return false;
    }

    public View getView() {
        return mLayout;
    }

    public void setNetCallback(NetCallback cb) {
        netCallback = cb;
    }

    public void setUpdateViewCallback(TitleBarPresenter.UpdateViewCallback cb) {
        mUpdateViewCallback = cb;
    }
}
